var querystring = require('querystring');
//模拟单机模式，参数虚构
var options ={'isHttps':false,'host':'127.0.0.1','port':'443','cookie':{'aaa':'bbb','ccc':'ddd'},'headers':{"Content-Type": "application/json",
    "User-Agent": 'Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit'},'body':{
    "bundleId": "com.denachina.pickle",
    "ip": "192.168.1.200",
    "sdkVersion": "1.0",
    'method':true,
    'str_':"aaaaa1",
    '&sig':"AAA"
}};
var method =options['method']? 'POST' :"GET";
options['isHttps'] = options['isHttps'] || false; //转换成布尔类型
//console.log(typeof(!!options['host']));
var httpopt =options['isHttps'] ? 'https://' : 'http://';  //可以用来切换httprequest里面的协议
var path1 =options['host']+':'+options['port']+"?";
console.log(httpopt); //false的三元判断为http://
console.log(path1);
//console.log(typeof(!options['headers']));
if (!!options['headers']) {
    var headers ={};
    for (const key in options['headers']) { //遍历
        headers[key]= options['headers'][key];
        console.log(headers[key]);
    }
}
var self = this;
if (!!options['host']) {
    self.host = options['host'];
}
if(!!options['port']) self.port = options['port'];
console.log(self.host,self.port);

var base64Encode = function (str) {
    var buf = new Buffer(str);
    return buf.toString("base64")
};

if (!!options['body']) {
    self.str_ = querystring.stringify(options['str_']).replace('%20', ' ');//%20为空格编码
    console.log(4);
    if (method=== "GET") {
        self.str = base64Encode(self.str_); //这里str和options里面的str需要看具体业务。
        //console.log('!!',self.str_);
        self.path += 'str=' + self.str;
        //self.host += 'host'+self.host;
        self.path += '&port'+self.port;
        //self.path += '&sig=' + self.sig;
        self.reqstr = querystring.stringify(options['body']);
        self.path += '&';//每段里面加1个&
        self.path += self.reqstr;
        console.log('self path =>', self.host + '/' + self.path + '<=') //输出还有问题
        //self path => 127.0.0.1/undefinedstr=&port443&bundleId=com.denachina.pickle&ip=192.168.1.5&sdkVersion=1.0&method=true&str_=aaaaa1&%26sig=AAA<=
    }
 }